﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataGridViewDataSource
{
    public partial class Form1 : Form
    {
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();

            dt.Columns.Add(new DataColumn("Type", typeof(string)) { Caption = "数据源类型" });
            dt.Columns.Add(new DataColumn("Id", typeof(int)) { Caption = "编号" });
            dt.Columns.Add(new DataColumn("Name", typeof(string)) { Caption = "姓名" });

            dt.Rows.Add("DataTable", 1, "张三");
            dt.Rows.Add("DataTable", 2, "李四");
            dt.Rows.Add(null, null, null);

            ds.Tables.Add(dt);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            // 禁用自动生成列(必须加,否则重新绑定数据时会用Type、Id、Name的DataTable列名)
            dgv.AutoGenerateColumns = false;

            // 禁用用户添加行
            dgv.AllowUserToAddRows = false;

            dgv.Columns[0].HeaderText = "数据源类型";
            dgv.Columns[0].DataPropertyName = "Type";
            dgv.Columns[1].HeaderText = "编号";
            dgv.Columns[1].DataPropertyName = "Id";
            dgv.Columns[2].HeaderText = "姓名";
            dgv.Columns[2].DataPropertyName = "Name";

            // 设置列标题字体和大小
            dgv.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 25);

            // 设置
            dgv.DefaultCellStyle.Font = new Font("Arial", 25);

            // 让行的高度根据内容自动调整
            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            // 让列的宽度根据内容自动调整
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;


            // 调整所有列的宽度
            //foreach (DataGridViewColumn item in dgv.Columns)
            //{
            //    item.Width = 150;
            //}

            //dgv.Columns[0].Width = 300;
        }

        /// <summary>
        /// DataTable作为数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            dgv.DataSource = null;
            foreach (DataRow dr in dt.Rows)
            {
                dr["Type"] = "DataTable";
            }
            dgv.DataSource = dt;
        }

        /// <summary>
        /// DataSource作为数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            dgv.DataSource = null;
            dgv.DataSource = ds;
            foreach (DataRow dr in ds.Tables["Table1"].Rows)
            {
                dr["Type"] = "DataSet";
            }
            dgv.DataMember = "Table1";
        }

        /// <summary>
        /// DataView作为数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            dgv.DataSource = null;
            foreach (DataRow dr in dt.Rows)
            {
                dr["Type"] = "DataView";
            }
            DataView dv = new DataView(dt);
            dgv.DataSource = dv;
        }

        /// <summary>
        /// List<T>作为数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            List<DataInfo> list = ConvertDataTableHelper.DataTableToList<DataInfo>(dt);
            foreach (var item in list)
            {
                item.Type = "List<T>";
            }
            dgv.DataSource = null;
            dgv.DataSource = list;
        }

        /// <summary>
        /// ArrayList作为数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ArrayList list = ConvertDataTableHelper.DataTableToArrayList<DataInfo>(dt);
            foreach (var item in list)
            {
                DataInfo data = item as DataInfo;
                data.Type = "ArrayList";
            }
            dgv.DataSource = null;
            dgv.DataSource = list;
        }

        /// <summary>
        /// 将DataTable转为List<Hashtable>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            List<Hashtable> hashtables = ConvertDataTableHelper.DataTableToListHashtable(dt);
        }
    }
}
